package agenti;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

import java.util.LinkedList;
import java.util.List;

import org.jfree.data.general.Dataset;

import xml.XMLWriter;

import componenti.buffer.Entity;
import componenti.comunication.OggettoSalvataggio;
import componenti.diagramDesigner.DiagramDesigner;

@SuppressWarnings("serial")
public class AgenteMonitoraggio extends Agent {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6390797520422247645L;
	private List<Entity> entity;
	private List<Dataset> dataset;

	@Override
	protected void setup() {
		super.setup();
		dataset = new LinkedList<Dataset>();
		entity = new LinkedList<Entity>();
		addBehaviour(new ChooseDiagram());

	}

	public void refreshDatasets(Entity e) {
		for (Dataset d : dataset) {
			// TODO fare istanceof per vedere come aggiungere i diversi elementi
			// ai
			// dataset
		}
	}

	private class ChooseDiagram extends CyclicBehaviour {

		/**
		 * 
		 */
		private static final long serialVersionUID = 384696856694062327L;

		@Override
		public void action() {
			MessageTemplate mt = MessageTemplate.and(MessageTemplate
					.MatchPerformative(ACLMessage.REQUEST), MessageTemplate
					.MatchSender(new AID("Requestor", AID.ISLOCALNAME)));
			ACLMessage msg = myAgent.receive(mt);
			MessageTemplate mt1 = MessageTemplate
					.MatchPerformative(ACLMessage.INFORM);
			ACLMessage msg1 = myAgent.receive(mt1);
			MessageTemplate mt2 = MessageTemplate.and(MessageTemplate
					.MatchPerformative(ACLMessage.CANCEL), MessageTemplate
					.MatchSender(new AID("Produttore", AID.ISLOCALNAME)));
			ACLMessage msg2 = myAgent.receive(mt2);
			MessageTemplate mt3 = MessageTemplate.and(MessageTemplate
					.MatchPerformative(ACLMessage.REQUEST), MessageTemplate
					.MatchSender(new AID("Save", AID.ISLOCALNAME)));
			ACLMessage msg3 = myAgent.receive(mt3);
			if (msg != null) {
				try {
					DiagramDesigner dd = (DiagramDesigner) msg
							.getContentObject();
					// questo metodo sa come disegnare il diagramma rispetto
					// alla simulazione passata come parametro
					dd.design(entity);
					dataset.add(dd.getDataset());
					dd.visualizza();
				} catch (UnreadableException e) {
					e.printStackTrace();
				}
			} else if (msg1 != null) {
				try {
					Entity e = (Entity) msg1.getContentObject();
					entity.add(e);
					refreshDatasets(e);
				} catch (UnreadableException e) {
					e.printStackTrace();
				}
			} else if (msg2 != null) {
				entity = new LinkedList<Entity>();
			} else if (msg3 != null) {
				OggettoSalvataggio o;
				try {
					o = (OggettoSalvataggio) msg3.getContentObject();
					XMLWriter xml = new XMLWriter(entity, o.getProduttore(),
							o.getConsumatore());
					xml.create(o.getNomeFile());
				} catch (UnreadableException e) {
					e.printStackTrace();
				}
			} else {
				block();
			}
		}

		private double[] getInizioFine(String s) {
			double[] res = new double[2];
			int blank = s.indexOf(" ");
			String inizio = s.substring(0, blank);
			String fine = s.substring(blank + 1, s.length());
			res[0] = Double.parseDouble(inizio);
			res[1] = Double.parseDouble(fine);
			return res;
		}
	}
}
